﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.UI.Models;
using System.Windows.Input;
using ARR.Shared;
using System.Collections.ObjectModel;
using ARR.Entities;
using System.Windows;
using ARR.UI.Commands;
using ARR.UI.EnvoyImporterService;
using ARR.UI.ArticlesService;
using ARR.UI.ExcelImporterService;
using System.ServiceModel;
using ARR.UI.Views;

namespace ARR.UI.ViewModels
{
    public class HistoryImporterViewModel : WorkspaceViewModel
    {
        public IHistoryImporterModel model;
        private ICommand cancelCommand;
        private ICommand nextCommand;
        private ICommand retryCommand;
        private ICommand startCommand;

        public HistoryImporterViewModel(IHistoryImporterModel model)
        {
            base.DisplayName = Labels.ImportadorDeHistoria;
            this.model = model;

            CurrentStep = 0;
        }

        public ICommand NextCommand
        {
            get
            {
                if (nextCommand == null)
                {
                    nextCommand = new RelayCommand(param => NextStep(), p => (HasPassedCurrentStep.HasValue && HasPassedCurrentStep.Value && CurrentStep != 4));
                }
                return nextCommand;
            }
        }

        public ICommand RetryCommand
        {
            get
            {
                if (retryCommand == null)
                {
                    retryCommand = new RelayCommand(param => RetryCurrentStep(), p => (HasPassedCurrentStep.HasValue && !HasPassedCurrentStep.Value));
                }
                return retryCommand;
            }
        }

        public ICommand StartCommand
        {
            get
            {
                if (startCommand == null)
                {
                    startCommand = new RelayCommand(param => NextStep(), p => CurrentStep == 0);

                }
                return startCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(param => Cancel());
                }
                return cancelCommand;
            }
        }

        public ObservableCollection<ImporterResult> Results
        {
            get
            {
                return new ObservableCollection<ImporterResult>(model.Results.OrderBy(x => x.Description));
            }
        }

        public Visibility IsVisibleResultsGV
        {
            get
            {
                if (Results != null && Results.Any())
                {
                    return Visibility.Visible;
                }

                return Visibility.Collapsed;
            }
        }

        public string Step1ImagePath { get; set; }

        public string Step2ImagePath { get; set; }

        public string Step3ImagePath { get; set; }

        public string Step4ImagePath { get; set; }

        public int CurrentStep { get; set; }

        public bool? HasPassedCurrentStep { get; set; }

        public bool IsBusy { get; set; }

        private void Cancel()
        {

        }

        private void NextStep()
        {
            switch (CurrentStep)
            {
                case 0:
                    RunStep1();
                    break;
                case 1:
                    RunStep2();
                    break;
                case 2:
                    RunStep3();
                    break;
                case 3:
                    RunStep4();
                    break;
            }


            OnPropertyChanged<HistoryImporterViewModel>(p => p.Results);
            OnPropertyChanged<HistoryImporterViewModel>(p => p.IsVisibleResultsGV);

            if (CurrentStep < 4)
                CurrentStep += 1;
        }

        private void RetryCurrentStep()
        {
            CurrentStep -= 1;
            NextStep();
        }

        private void RunStep1()
        {
            IsBusy = true;
            ExcelImporterServiceClient excelService = new ExcelImporterServiceClient();

            try
            {
                excelService.ImportHistoryStep1Completed += (excelService_ImportHistoryStep1Completed);
                excelService.ImportHistoryStep1Async(WorkspaceData.Instance.RangeReview);
            }
            catch (Exception ex)
            {
                excelService.Abort();
                throw ex;
            }

            OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);
        }

        private void RunStep2()
        {
            IsBusy = true;
            ExcelImporterServiceClient excelService = new ExcelImporterServiceClient();

            try
            {
                excelService.ImportHistoryStep2Completed += (excelService_ImportHistoryStep2Completed);
                excelService.ImportHistoryStep2Async(WorkspaceData.Instance.RangeReview);
            }
            catch (Exception ex)
            {
                excelService.Abort();
                throw ex;
            }

            OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);
        }

        private void RunStep3()
        {
            ExcelImporterServiceClient excelService = new ExcelImporterServiceClient();

            try
            {
                excelService.ImportHistoryStep3Completed += (excelService_ImportHistoryStep3Completed);
                excelService.ImportHistoryStep3Async(WorkspaceData.Instance.RangeReview);
            }
            catch (Exception ex)
            {
                excelService.Abort();
                throw ex;
            }

            IsBusy = true;
            OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);

        }

        private void RunStep4()
        {
            ExcelImporterServiceClient excelService = new ExcelImporterServiceClient();

            try
            {
                excelService.ImportHistoryStep4Completed += (excelService_ImportHistoryStep4Completed);
                excelService.ImportHistoryStep4Async(WorkspaceData.Instance.RangeReview);
            }
            catch (Exception ex)
            {
                excelService.Abort();
                throw ex;
            }

            IsBusy = true;
            OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);
        }

        void excelService_ImportHistoryStep1Completed(object sender, ImportHistoryStep1CompletedEventArgs e)
        {
            IsBusy = false;
            OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);

            if (e.Error == null)
            {
                (sender as ICommunicationObject).Close();
                model.Results = e.Result;

                Step1ImagePath = "/Images/Pass-16.png";
                HasPassedCurrentStep = true;

                if (model.Results.Any())
                {
                    if (model.Results.Any(p => p.ErrorType == ErrorType.Error))
                    {
                        Step1ImagePath = "/Images/NotPass-16.png";
                        HasPassedCurrentStep = false;
                    }
                }

                IsBusy = false;
                OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.HasPassedCurrentStep);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.Step1ImagePath);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.Results);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.IsVisibleResultsGV);
            }
            else
            {
                (sender as ICommunicationObject).Abort();
                throw e.Error;
            }
                       
        }

        void excelService_ImportHistoryStep2Completed(object sender, ImportHistoryStep2CompletedEventArgs e)
        {
            IsBusy = false;
            OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);

            if (e.Error == null)
            {
                (sender as ICommunicationObject).Close();
                model.Results = e.Result;

                Step2ImagePath = "/Images/Pass-16.png";
                HasPassedCurrentStep = true;

                if (model.Results.Any())
                {
                    if (model.Results.Any(p => p.ErrorType == ErrorType.Error))
                    {
                        Step2ImagePath = "/Images/NotPass-16.png";
                        HasPassedCurrentStep = false;
                    }
                }

                IsBusy = false;
                OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.HasPassedCurrentStep);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.Step2ImagePath);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.Results);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.IsVisibleResultsGV);
            }
            else
            {
                (sender as ICommunicationObject).Abort();
                throw e.Error;
            }

            
        }

        void excelService_ImportHistoryStep3Completed(object sender, ImportHistoryStep3CompletedEventArgs e)
        {
            IsBusy = false;
            OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);
            
            if (e.Error == null)
            {
                (sender as ICommunicationObject).Close();
                model.Results = e.Result;

                Step3ImagePath = "/Images/Pass-16.png";
                HasPassedCurrentStep = true;

                if (model.Results.Any())
                {
                    if (model.Results.Any(p => p.ErrorType == ErrorType.Error))
                    {
                        Step3ImagePath = "/Images/NotPass-16.png";
                        HasPassedCurrentStep = false;
                    }
                }

                IsBusy = false;
                OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.HasPassedCurrentStep);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.Step3ImagePath);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.Results);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.IsVisibleResultsGV);
            }
            else
            {
                (sender as ICommunicationObject).Abort();
                throw e.Error;
            }

           
        }

        void excelService_ImportHistoryStep4Completed(object sender, ImportHistoryStep4CompletedEventArgs e)
        {
            IsBusy = false;
            OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);

            if (e.Error == null)
            {
                (sender as ICommunicationObject).Close();
                model.Results = e.Result;

                Step4ImagePath = "/Images/Pass-16.png";
                HasPassedCurrentStep = true;

                if (model.Results.Any())
                {
                    if (model.Results.Any(p => p.ErrorType == ErrorType.Error))
                    {
                        Step4ImagePath = "/Images/NotPass-16.png";
                        HasPassedCurrentStep = false;
                    }
                }

                IsBusy = false;
                OnPropertyChanged<HistoryImporterViewModel>(p => p.IsBusy);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.HasPassedCurrentStep);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.Step4ImagePath);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.Results);
                OnPropertyChanged<HistoryImporterViewModel>(p => p.IsVisibleResultsGV);
            }
            else
            {
                (sender as ICommunicationObject).Abort();
                throw e.Error;
            }
            
        }

    }
}
